#pragma once
#include <thread>
#include <atomic>
#include "LockFreeRingBuffer.h"
#include "MemoryPool.h"
#include "OrderPacket.h"

class Receiver {
public:
    Receiver(const char* ip, uint16_t port, 
             LockFreeRingBuffer<OrderPacket>& ringBuffer,
             MemoryPool& memoryPool,
             int cpu_id);

    ~Receiver();

    void start();
    void stop();

    // bool initSocket(); // 初始化套接字时设置 SO_TIMESTAMPING
    // bool extractTimestamp(const struct msghdr& msg, uint64_t& hwTimestampNs);

private:
    void run();
    void handlePacket(const char* data, size_t len);

    int sockfd_;
    std::thread thread_;
    std::atomic<bool> running_{false};

    LockFreeRingBuffer<OrderPacket>& ringBuffer_;
    MemoryPool& memoryPool_;
    int cpu_id_;
};
